﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using VietPublisher.Membership;
using VietPublisher.API.Membership;
using VietPublisher.BSL.BK;
using System.Transactions;
using System.Web.Security;
using VietPublisher.FileManager;
using VietPublisher.Repository.Repositories.Concrete;




namespace VietPublisher.Repositories.Interface
{
    public class AccountRepository : BaseRepository, IAccountRepository
    {
        public AccountRepository() { }

        public List<T_BK_UserExtend> getAccountList()
        {
            var userExtends = ContextBookStore.T_BK_UserExtend.ToList();
            return userExtends;
        }

        public T_BK_UserExtend getAccountByUserName(string userName)
        {
            var userExtend = ContextBookStore.T_BK_UserExtend.Where(p => p.UserName == userName).FirstOrDefault();
            return userExtend;            
        }

        public bool CreateAccount(T_BK_UserExtend account, string Password , VietPublisher.BaseProject.Enums.UserTypes UserTypes  )
        {
            try
            {
               // using (TransactionScope scope = new TransactionScope())
               // {
               
                var user = ContextMembership.aspnet_Users.FirstOrDefault(m => m.UserName == account.UserName);

                var result = Membership.CreateUser(account.UserName, Password, account.Email, UserTypes);
                    if (result != MembershipCreateStatus.Success)
                    {
                        return false;
                    }
                 
                    if (user != null)
                    {
                        var userInsert = new T_BK_UserExtend()
                        {
                            UserID = user.UserId,
                            UserName= account.UserName,
                            Address = account.Address,
                            BirthDate = account.BirthDate,
                            Email = account.Email,
                            IsMale = account.IsMale,
                            MobilePhone = account.MobilePhone,
                            Name = account.Name,
                            UpdateDate = DateTime.Now,
                            UserTypes = (int)UserTypes,
                            UseYN = true
                        };

                        ContextBookStore.T_BK_UserExtend.AddObject(userInsert);
                        ContextMembership.SaveChanges();
                    }
                 //   scope.Complete();
               // }

                return true;
            }
            catch (Exception e)
            {
                LogUtility.LogMessage("AccountRepository", "CreateAccount", e.Message);
                return false;
            }

            
        }

        public int UpdateAccount(T_BK_UserExtend account)
        {
            throw new NotImplementedException();
        }

        public bool DeleteAccount(string userName)
        {
            throw new NotImplementedException();
        }
        
        public bool ChangePassword(string userName, string oldPass, string newPass)
        {
            bool result = false;
            try
            {
               result= Membership.ChangePassword(userName, oldPass, newPass);
            }
            catch (Exception e)
            {
                LogUtility.LogMessage("AccountRepository", "ChangePassword", e.Message);
                result = false;
            }
            return result;            
        }

        public bool Login(string userName, string passWord)
        {
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(passWord))
            {
                throw new Exception("UserName and Password is not valid");
            }

           var result= Membership.ValidateUser(userName, passWord);
           if (result == BaseProject.UserValidateStatus.Success)
           {
               if (GetUserTypeBy(userName) == BaseProject.Enums.UserTypes.ADMIN)
               {
                   return true;
               }
               return false;
           }
           else
           {
               return false;
           }
        }


        public bool LoginCustomer(string userName, string passWord)
        {
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(passWord))
            {
                throw new Exception("UserName and Password is not valid");
            }

            var result = Membership.ValidateUser(userName, passWord);
            if (result == BaseProject.UserValidateStatus.Success)
            {
                if (GetUserTypeBy(userName) == BaseProject.Enums.UserTypes.CUSTOMER) {
                    return true;
                }

                return false;
            }
            else
            {
                return false;
            }
        }


        public BaseProject.Enums.UserTypes GetUserTypeBy(string userName)
        {
            int user =(int)BaseProject.Enums.UserTypes.CUSTOMER;

            var usr = ContextBookStore.T_BK_UserExtend.Where(p =>p.UserName == userName).FirstOrDefault();
            if (usr.UserTypes == (int)BaseProject.Enums.UserTypes.ADMIN)
            {
                return BaseProject.Enums.UserTypes.ADMIN;
            }
            else {
                return BaseProject.Enums.UserTypes.CUSTOMER;
            }
            
        }
    }
}
